﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Pigs.Enemies
{
    /*
     * Suicide AI and update need to be moved into here so we can clean up Pig.cs
     * 
     */

    class SuicidePig : pigs.Pig
    {
        // Animation variables
        int frameCount = 0; // Current frame
        int frameSkipX = 84; // Width of a frame
        int animationCount; // How many ticks since the last frame change.
        int animationMax; // How many ticks to change frame after. 
        Rectangle source;

        public SuicidePig(Texture2D texture, Vector2 position, int numPlayers)
            : base(texture, position)
        {
            START_HEALTH = 5;
            START_DAMAGE = 10;
            START_SPEED = 4f;
            VALUE = 500;

            this.shouldEatSlop = true;
            this.position = position;
            this.active = true; // We initialize the enemy to be active so it will be update in the game
            if (numPlayers == 1) { this.health = START_HEALTH; }
            else if (numPlayers > 1 && numPlayers < 3) { this.health = START_HEALTH * 2; }
            else { this.health = this.START_HEALTH * 3; }
            if (numPlayers == 1) { this.damage = this.START_DAMAGE; }
            else if (numPlayers > 2 && numPlayers < 5) { this.damage = this.START_DAMAGE + 6; }
            else { this.damage = this.START_DAMAGE + 11; }
            this.speed = START_SPEED;
            this.value = VALUE;
            this.effect = SpriteEffects.None;
            this.speed = START_SPEED;
            this.ID = "suicide";
            this.vertSpeed = 5;
            this.lead = false;
            this.buff = false;
            this.escaped = false;
            this.hitByFlame = false;
            this.ghost = false;

            // Start with frame 1 of image array
            this.frameCount = 1;
            this.animationCount = 0;
            this.animationMax = 3;

            this.source = new Rectangle(this.frameSkipX * this.frameCount, 0, this.frameSkipX, this.texture.Height);

            rectangle = new Rectangle((int)position.X, (int)position.Y, source.Width, source.Height);

            //get the color data
            colorMap = new Color[source.Width * source.Height];
            this.texture.GetData(0, source, colorMap, 0, source.Width * source.Height);
        }

        public void doAI(GameTime gameTime, List<pigs.Scenery> obstacles, Vector2 pos)
        {
            // If a suicide pig gets a leader buff, double its damage, life and increase its speed by 1
            if (this.lead && !this.buff)
            {
                this.health *= 2;
                this.damage *= 2;
                this.speed += 1;
                this.buff = true;
            }
            if (this.buff)
            {
                // Also, with a leader buff, make it smart enough to avoid obstacles
                if (!this.avoidScenery(obstacles))
                {
                    float xTogo = pos.X - position.X;
                    float yTogo = pos.Y - position.Y;

                    Vector2 step = new Vector2((float)(xTogo / Math.Sqrt(xTogo * xTogo + yTogo * yTogo)),
                        (float)(yTogo / Math.Sqrt(xTogo * xTogo + yTogo * yTogo)));

                    if (step.X > 0)
                    {
                        this.position.X -= 1;
                    }
                    else
                    {
                        this.position.X += step.X * START_SPEED;
                    }

                    if (this.position.Y > pos.Y)
                    {
                        if (ObstacleMove(obstacles, "up", this.position).Equals("none"))
                        {
                            this.position.Y += step.Y * speed;
                        }
                    }
                    else
                    {
                        if (ObstacleMove(obstacles, "down", this.position).Equals("none"))
                        {
                            this.position.Y += step.Y * speed;
                        }
                    }

                    // If the pig's health is <= 0, then it should die
                    if (this.health <= 0)
                    {
                        this.active = false;
                    }
                    rectangle = new Rectangle((int)position.X, (int)position.Y, Width, Height);
                }
            }
            else
            {
                // Otherwise shoot it at the player
                float xTogo = pos.X - position.X;
                float yTogo = pos.Y - position.Y;

                Vector2 step = new Vector2((float)(xTogo / Math.Sqrt(xTogo * xTogo + yTogo * yTogo)),
                    (float)(yTogo / Math.Sqrt(xTogo * xTogo + yTogo * yTogo)));

                if (step.X > 0)
                {
                    this.position.X -= 1;
                }
                else
                {
                    this.position.X += step.X * START_SPEED;
                }

                this.position.Y += step.Y * speed;

                // If the pig's health is <= 0, then it should die
                if (this.health <= 0)
                {
                    this.active = false;
                }
                rectangle = new Rectangle((int)position.X, (int)position.Y, Width, Height);
            }
        }

        public override void suiUpdate(GameTime gameTime, List<pigs.Scenery> obstacles, Vector2 pos)
        {
            if (this.shouldGhost && this.color.A > 70)
            {
                this.color.A--;
                this.color.A--;
                this.color.A--;
            }
            this.doAI(gameTime, obstacles, pos);
            rectangle = new Rectangle((int)this.position.X, (int)this.position.Y, source.Width, source.Height);
            UpdateAnimation();
        }

        // Perform the animation updates on the image
        public void UpdateAnimation()
        {
            this.animationCount += 1;
            if (this.animationCount > this.animationMax)
            {
                this.animationCount = 0;
                this.frameCount += 1;
                if (this.frameCount == 5)
                {
                    this.frameCount = 0;
                }
            }
        }

        //Draw the updated texture image
        public override void Draw(SpriteBatch spriteBatch)
        {
            // Update the source rectangle, based on where in the animation we are.  
            this.source.X = this.frameSkipX * this.frameCount;

            spriteBatch.Draw(texture, position, this.source, color, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
        }
    }
}
